﻿using System;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
namespace Common.Net
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    public static class SendMail
    {
        /// <summary>
        /// 向单人发送邮件
        /// </summary>
        /// <param name="mailaccount">邮件账户</param>
        /// <param name="password">密码</param>
        /// <param name="host">邮件服务器mail地址</param>
        /// <param name="s_to">收件人</param>
        /// <param name="s_body">正文</param>
        /// <param name="title">标题</param>
        /// <param name="files">附件</param>
        public static void SendMailTo(string mailaccount, string password, string host, string s_to, string s_body, string title, string[] files)
        {
            if (!string.IsNullOrEmpty(s_to))
            {
                string s_from = mailaccount;
                string pwd = password;
                int i = s_from.IndexOf("@");
                string username = s_from.Substring(0, i);
                MailAddress from = new MailAddress(s_from);
                MailAddress to = new MailAddress(s_to);
                MailMessage mailobj = new MailMessage(from, to);
                mailobj.Subject = title;
                mailobj.Body = s_body;
                mailobj.IsBodyHtml = true;
                mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mailobj.Priority = MailPriority.High;
                SmtpClient smtp = new SmtpClient(host);
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(username, pwd);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                if (files != null)
                {
                    if (files.Length > 0)          //添加附件
                    {
                        for (int ia = 0; ia < files.Length; ia++)
                        {
                            AddAttachment(files[ia], mailobj);
                        }
                    }
                }
                try
                {
                    smtp.Send(mailobj);
                }
                catch
                {

                }
            }

        }
        /// <summary>
        /// 向多人发送邮件
        /// </summary>
        /// <param name="host">邮件服务器mail地址</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="mailaccount">邮件账户</param>
        /// <param name="to">收件人地址</param>
        /// <param name="s_body">邮件内容</param>
        /// <param name="title">邮件标题</param>
        /// <param name="files">附件</param>
        public static void SendMorePersion(string host, string username, string password, string mailaccount, string[] to, string s_body, string title, string[] files)
        {
            SmtpClient smtp = new SmtpClient();
            MailMessage mail = new MailMessage();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            for (int i = 0; i < to.Length; i++)
            {

                smtp.Host = host;
                smtp.Credentials = new System.Net.NetworkCredential(username, password);
                mail.From = new MailAddress(mailaccount);
                mail.To.Add(to[i]);
                mail.Subject = title;
                mail.Body = s_body;
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.Normal;



            }
            if (files != null)
            {
                if (files.Length > 0)          //添加附件
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        AddAttachment(files[i], mail);
                    }
                }
            }
            smtp.Send(mail);


        }
        /// <summary>  
        /// 添加附件  
        /// </summary>  
        private static void AddAttachment(string Path, MailMessage m)
        {
            string[] path = Path.Split(',');
            Attachment data;
            ContentDisposition disposition;
            for (int i = 0; i < path.Length; i++)
            {
                data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化 附件  
                disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取 附件的创建日期  
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);// 获取附件的修改日期  
                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附 件的读取日期  
                m.Attachments.Add(data);//添加到附件中  
            }
        }

    }
}
